import { createRouter, createWebHistory } from "vue-router";

import { onBuriedPoint } from "@/api/user";
import MainLayout from "@/layout/main-layout/index.vue";
import { progress } from "@/plugin/nprogress";
import { useAuth } from "@/store/auth";
import { useBasic } from "@/store/basic";

export const userCenterRoutes = [
  {
    path: "/profile",
    meta: {
      name: "Profile",
      requireAuth: true,
    },
    component: () => import("@/views/account/profile/index.vue"),
  },
  {
    path: "/deposit",
    meta: {
      name: "Deposit",
      requireAuth: true,
    },
    component: () => import("@/views/account/deposit/index.vue"),
  },
  {
    path: "/withdrawal",
    meta: {
      name: "Withdrawal",
      requireAuth: true,
    },
    component: () => import("@/views/account/withdrawal/index.vue"),
  },
  {
    path: "/sales",
    meta: {
      name: "Sales",
      requireAuth: true,
    },
    component: () => import("@/views/account/sales/index.vue"),
  },
  {
    path: "/box-history",
    meta: {
      name: "Box History",
      requireAuth: true,
    },
    component: () => import("@/views/account/box-history/index.vue"),
  },
  {
    path: "/battle-history",
    meta: {
      name: "Battle History",
      requireAuth: true,
    },
    component: () => import("@/views/account/battle-history/index.vue"),
  },
  {
    path: "/forge-history",
    meta: {
      name: "Forge History",
      requireAuth: true,
    },
    component: () => import("@/views/account/forge-history/index.vue"),
  },
  {
    path: "/my-deliveries",
    meta: {
      name: "My Deliveries",
      requireAuth: true,
    },
    component: () => import("@/views/account/my-deliveries/index.vue"),
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: "/",
      meta: {
        name: "首页",
      },
      component: MainLayout,
      children: [
        {
          path: "/",
          meta: {
            name: "首页",
          },
          component: () => import("@/views/home/index.vue"),
        },
        {
          path: "/boxs",
          meta: {
            name: "boxes-列表页",
          },
          component: () => import("@/views/boxs/boxs/index.vue"),
        },
        {
          path: "/boxs/:box_id",
          meta: {
            name: "boxes-详情页",
          },
          component: () => import("@/views/boxs/boxs-item/index.vue"),
        },
        {
          path: "/battles",
          meta: {
            name: "battles-首页",
          },
          component: () => import("@/views/battles/index.vue"),
        },
        {
          path: "/battles/:battle_id",
          meta: {
            name: "battles-详情",
          },
          component: () => import("@/views/battles/detail/index.vue"),
        },
        {
          path: "/forge",
          meta: {
            name: "forge-详情页",
          },
          component: () => import("@/views/forge/index.vue"),
        },
        {
          path: "/events",
          meta: {
            name: "events-首页",
          },
          component: () => import("@/views/events/index.vue"),
        },
        {
          path: "/account",
          redirect: userCenterRoutes[0].path,
          component: () =>
            import("@/layout/user-center-layout/user-center-layout.vue"),
          children: [...userCenterRoutes],
        },
      ],
    },
  ],
});

router.beforeEach(async (from, to) => {
  const basic = useBasic();
  const auth = useAuth();

  basic.getBottomInfo();
  if (from.path !== to.path) {
    progress.start();
  }

  if (from.fullPath !== to.fullPath) {
    onBuriedPoint({
      page_name: from.meta.name as string,
      action: "页面加载",
      ...(from.params || {}),
      ...(from.query || {}),
    });
  }

  if (auth.userToken) {
    if (Object.keys(auth.userInfo).length === 0) {
      await auth.getUserBasicInfo();
    }
  } else {
    if (from.meta?.requireAuth === true) {
      return "/";
    }
  }

  return true;
});

router.afterEach((from, to) => {
  if (from.path !== to.path) {
    window.scrollTo(0, 0);
  }
  progress.done();
});

export default router;
